package com.gh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gh.bo.TransFlowBO;
import com.gh.entity.TransFlow;
import com.gh.entity.UserInfo;
import com.gh.mapper.TransFlowMapper;
import com.gh.mapper.UserInfoMapper;
import com.gh.service.TransFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author Eric
 * @since 2021-09-06
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void saveTransFlow(TransFlowBO transFlowBO) {
        String bindCode = transFlowBO.getBindCode();
        //根据bindCode查询用户id
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getBindCode, bindCode);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        //实例化流水对象
        TransFlow transFlow = new TransFlow();
        transFlow.setTransAmount(transFlowBO.getAmount());
        transFlow.setMemo(transFlowBO.getMemo());
        transFlow.setTransTypeName(transFlowBO.getTransTypeEnum().getTransTypeName());
        transFlow.setTransType(transFlowBO.getTransTypeEnum().getTransType());
        transFlow.setTransNo(transFlowBO.getAgentBillNo());//流水号
        transFlow.setUserId(userInfo.getId());
        transFlow.setUserName(userInfo.getName());
        //保存交易流水
        baseMapper.insert(transFlow);
    }

    @Override
    public boolean isSaveTransFlow(String agentBillNo) {
        LambdaQueryWrapper<TransFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TransFlow::getTransNo, agentBillNo);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }

    @Override
    public List<TransFlow> selectByUserId(Long userId) {
        LambdaQueryWrapper<TransFlow> queryWrapper = new QueryWrapper<TransFlow>().lambda().eq(TransFlow::getUserId, userId).orderByDesc(TransFlow::getCreateTime);
        List<TransFlow> transFlowList = baseMapper.selectList(queryWrapper);
        return transFlowList;
    }
}
